
/**********************************************************************************************************************
  COPYRIGHT
-----------------------------------------------------------------------------------------------------------------------
  \par      copyright
  \verbatim
  Copyright (c) 2018 by Vector Informatik GmbH.                                                  All rights reserved.

                This software is copyright protected and proprietary to Vector Informatik GmbH.
                Vector Informatik GmbH grants to you only those rights as set out in the license conditions.
                All other rights remain with Vector Informatik GmbH.
  \endverbatim
-----------------------------------------------------------------------------------------------------------------------
  FILE DESCRIPTION
-----------------------------------------------------------------------------------------------------------------------
  \file  File:  BrsAsrMain.c
      Project:  Vector Basic Runtime System for MICROSAR4
       Module:  BrsAsrMain

  \brief Description:  Main File of BrsAsr4 contains
                       - Main function (start of initialization (EcuM)
                       - Default_Init_Task, Main and Background Task which can be used by the operating system
                       - Exception handling

  \attention Please note:
    The demo and example programs only show special aspects of the software. With regard to the fact
    that these programs are meant for demonstration purposes only, Vector Informatik liability shall be
    expressly excluded in cases of ordinary negligence, to the extent admissible by law or statute.
**********************************************************************************************************************/

/**********************************************************************************************************************
  AUTHOR IDENTITY
 ----------------------------------------------------------------------------------------------------------------------
  Name                          Initials      Company
  ----------------------------  ------------  -------------------------------------------------------------------------
  Benjamin Walter               visbwa        Vector Informatik GmbH
  Andreas Hahn                  vishan        Vector Informatik GmbH
-----------------------------------------------------------------------------------------------------------------------
  REVISION HISTORY
 ----------------------------------------------------------------------------------------------------------------------
  Version   Date        Author  Description
  --------  ----------  ------  ---------------------------------------------------------------------------------------
  01.00.00  2014-02-03  visbwa  Initial creation of new BrsAsr4 code basis
  01.00.01  2014-04-11  visbwa  Removed DeclareTask() calls as this is not needed by AsrOs
  01.00.02  2014-08-19  visbwa  Added for SafeContext and/or MultiCore OS (inclusive MemMap usage),
                                added legal wording within header information
  01.00.03  2014-09-11  visbwa  Encapsulated mainTask and backgroundTask to be configuration dependent,
                                added support for UseCase w/o OS
  01.00.04  2014-10-16  visbwa  IdleTask_Core0 renamed to IdleTask and always available with OS-support (for new OS),
                                osInitialize() always called with OS-support,
                                added support for MultiCore SafeWatchdog Stack
  01.00.05  2014-10-20  visbwa  encapsulated OS Hook templates by BRSASR_ENABLE_OSSUPPORT,
                                eliminated use of BRS_ENABLE_TIMER_INTERRUPT
  01.00.06  2015-02-06  visbwa  Replaced MSEC by OS_MS2TICKS to support new OS versions
  01.00.07  2015-02-09  visbwa  Replaced OS_MS2TICKS by OS_MS2TICKS_SystemTimer and added error-check,
                                added call of BrsHwPreInitPowerOn (encapsulated by BRSHW_PREINIT_AVAILABLE)
  01.00.08  2015-03-12  visbwa  Encapsuled call of BrsHwSetLed() with BRS_ENABLE_SUPPORT_LEDS
  01.00.09  2015-03-31  visbwa  Changed ECUM_CORE_ID_MASTER to ECUM_CORE_ID_STARTUP within main() for MultiCore
  01.00.10  2015-07-14  visbwa  Removed usage of v_cfg.h (replaced platform defines by compiler.h defines)
  01.01.00  2015-09-03  visbwa  Enhanced VTT support, removed CANoe-Emu support, added support of StartApplication,
                                removed OS-Hooks, as they are now generated by Os_Proxy within OS_Callout_Stubs.c
  01.01.01  2015-09-10  visbwa  Fixed VTT-filter bug (BrsAsrMainInit), 
                                added call of SchM_Init()(VTT-UseCase) and Rte_Start() to InitTasks for MultiCore
  01.01.02  2015-09-11  visbwa  Added sample for ESCAN00078832 workaround, fixed VTT MultiCore StartUp, 
                                included Rte_Main.h for MultiCore StartUp
  01.02.00  2015-10-06  visbwa  Enhanced TGF support, removed CpuLoad_Measurement
  01.02.01  2015-11-12  visbwa  Added call of osInitSlaveCore() for RH850 with MultiCore OS
  01.02.02  2015-11-26  visbwa  Fixed BRS_CPU_ defines to proper BRS_PLATFORM_ defines
  01.02.03  2015-12-02  visbwa  Readded CANoe-Emu support for Product/Program-Tests
  01.02.04  2015-12-09  visbwa  Added comment for RH850 OS function osInitINTC()
  01.02.05  2016-03-16  visbwa  Removed include of BrsAsrAppl.h for UseCase StartApplication,
                                fixed usage of zOs_Emu
  01.02.06  2016-03-23  visbwa  Added include of BrsAsrInit.h for UseCase BRSASR_ENABLE_SAFEWDGSUPPORT
  01.02.07  2016-03-30  visbwa  Fixed/disabled call of BrsHwTimeBaseInitPowerOn() in UseCase zOs_Emu
  01.02.08  2016-03-31  visbwa  Fixed initialization of gbBrsMainIsrMilliSecondFlag
  01.02.09  2016-05-24  visbwa  Introduced support for OsCoreGen7,
                                removed include of osekext.h (SafeContext), because Os.h should be sufficient anyway
  01.02.10  2016-06-13  visbwa  Fixed encapsulation of variables with BRS_ENABLE_SUPPORT_LEDS
  01.03.00  2016-08-18  vishan  Added support for TCM driver
  01.03.01  2016-09-27  visbwa  Fixed TCM support (mainTask also available, if LED-support switched off)
  01.03.02  2016-10-12  visbwa  Introduced support for OsCoreGen7 initialization
  01.03.03  2016-10-19  visbwa  Moved include of EcuM.h into BrsAsrMain.h
  01.03.04  2016-10-25  visbwa  Changed encapsulation of BrsHw timer handling to BRSHW_ENABLE_TIMER_INTERRUPT
  01.03.05  2016-12-13  vishan  Moved calls of Tcm_MainFunction() and Tcm_BackgroundFunction(),
                                added HLP Testsuite functions
  01.03.06  2016-12-21  visbwa  Fixed initialization of wBrsMainCallCounter10ms and encapsulation of mainTask
  01.03.07  2017-01-05  vishan  Added BrsTcc_MeasureCyclesPerDelayLoopInMSec() call in BrsAsrMainInit()
  01.03.08  2017-01-11  vishan  Changed encapsulation regarding BRS_ENABLE_HLPTEST_SUPPORT
  01.03.09  2017-04-06  visbwa  Removed call of osInitSlaveCore() for OsGen7@RH850
  01.03.10  2017-05-08  visbwa  Added call of CANoeAPI_ConsumeTicks() within backgroundTask for UseCase VTT
  01.03.11  2017-05-23  visbwa  Fixed encapsulation of backgroundTask within header
  01.05.00  2017-06-20  visbwa  Added support for TFW, removed outdated MultiConfig and CANoeEmu support,
                                renamed mainTask to BrsAsrMainTask and backgroundTask to BrsAsrMainBackgroundTask
  01.05.01  2017-10-04  visbwa  Renamed InitTask to Default_Init_Task (same naming as StartApplication),
                                removed outdated support for OsGen6-RH850-MultiCore-UseCase,
                                simplified filtering of VTT/non-VTT UseCase
  01.06.00  2017-10-10  visbwa  Removed usage of BrsAsrAppl -> added usage of BswInit_Callout_Stubs instead,
                                made BrsAsrMainInit() global (to be accessed from BswInit_Callout_Stubs),
                                moved several MainFunction calls into new structured BrsAsrMainCyclic1ms(),
                                reworked main() for complete usage of BswInit_Callout_Stubs
  01.06.01  2017-11-08  visbwa  Added encapsulation to BrsAsrMainCyclic1ms() and BrsAsrMainTask
  01.06.02  2017-11-29  visbwa  Enhanced encapsulation of BrsTcc parts for UseCase HlpTest w/o TCM
  01.06.03  2017-12-06  visbwa  Reworked sample/example code disclaimer
  01.06.04  2018-02-14  visbwa  Added include of CANoeAPI.h for UseCase VTT
  01.07.00  2018-04-03  visbwa  Removed BrsAsrInit, NvM_ReadAll() and SafeWd support (part of Vita now)
  01.08.00  2018-06-22  visbwa  Remove encapsulation of main() (now always available, also in UseCase StartApplication)
                                Added CAN stack exclusive area workaround from BswInit_Callout_Stubs.c
  01.08.01  2018-06-29  visbwa  Added encapsulation of int main(void) declaration in header: VTT not C-spec conform
                                (actual VTT implementation seems to require void main(void))
  01.08.02  2018-07-18  visbwa  Moved BrsHwDisableInterruptAtPowerOn() from BrsAsrMainInit() into main()
                                (OS specifies, this has to be called before Os_Init() and for every Core)
  01.08.03  2018-07-30  visbwa  Encapsuled call of BrsHwDisableInterruptAtPowerOn() with usecase != VTT
  01.09.00  2018-08-22  visbwa  Removed !=StartAppl encapsulation of Default_Init_Task (always necessary within >=R21)
**********************************************************************************************************************/

/**********************************************************************************************************************
*  EXAMPLE CODE ONLY
*  -------------------------------------------------------------------------------------------------------------------
*  This Example Code is only intended for illustrating an example of a possible BSW integration and BSW configuration.
*  The Example Code has not passed any quality control measures and may be incomplete. The Example Code is neither
*  intended nor qualified for use in series production. The Example Code as well as any of its modifications and/or
*  implementations must be tested with diligent care and must comply with all quality requirements which are necessary
*  according to the state of the art before their use.
*********************************************************************************************************************/

/**********************************************************************************************************************
  INCLUDES
**********************************************************************************************************************/


#include "Os.h"

/* Ecu State Manager has to be available in system (BSW module or BRS Stubb) */
#include "EcuM.h"
#if 0
#include "BrsAsrMain.h"

#include "BswInit_Callout_Stubs.h"

#include "VStdLib.h"

#if defined (BRS_ENABLE_HLPTEST_SUPPORT)
# if defined (BRS_ENABLE_TCM_SUPPORT)
  #include "BrsTcc.h"
# endif
  #include "HlpTest_Main.h"
#endif

#if defined (_MICROSOFT_C_VTT_)
  #include "CANoeAPI.h"
  #include <stdio.h>
#endif

#if !defined (_MICROSOFT_C_VTT_)
  #include "BrsHw.h"
#endif

#if defined (BRSASR_ENABLE_OS_MULTICORESUPPORT)
  #include "Rte_Main.h"
#endif

#if defined (BRSASR_ENABLE_TGFSUPPORT)
  #include "BrsTcc.h"
  #include "TgfMain.h"
#endif

#if defined (BRS_ENABLE_TCM_SUPPORT)
  #include "TcmDriver.h"
#endif

#if defined (BRSASR_ENABLE_TFW_SUPPORT)
  #include "TFW.h"
#endif

#if !defined (BRS_ENABLE_HLPTEST_SUPPORT)
  #include "Can.h"
#endif
#endif
/**********************************************************************************************************************
  VERSION CHECK
**********************************************************************************************************************/


/**********************************************************************************************************************
  CONFIGURATION CHECK
**********************************************************************************************************************/

/**********************************************************************************************************************
  DEFINITION + MACROS
**********************************************************************************************************************/

/**********************************************************************************************************************
  GLOBAL VARIABLES
**********************************************************************************************************************/


/**********************************************************************************************************************
  GLOBAL CONST VARIABLES
**********************************************************************************************************************/

/**********************************************************************************************************************
  LOCAL VARIABLES
**********************************************************************************************************************/
#define START_SEC_VAR_ZERO_INIT_16BIT
#include "MemMap.h"
/**
 * \var wBrsMainCallCounter1ms
 *      Counter for calls of the function BrsAsrMainCyclic1ms
 */
static volatile uint16 wBrsMainCallCounter1ms;

/**
 * \var wBrsMainCallCounter10ms
 *      Counter for calls of functions with 10ms cycletime
 */
static volatile uint16 wBrsMainCallCounter10ms;
#define STOP_SEC_VAR 
#include "MemMap.h"

# if defined (BRS_ENABLE_SUPPORT_LEDS)
#define START_SEC_VAR_ZERO_INIT_16BIT
#include "MemMap.h"
/**
 * \var bBrsMainToggleLedCounter
 *      Counter for LED toggling within BrsMainMilliSecondHandler
 */
static uint8 bBrsMainToggleLedCounter;
#define STOP_SEC_VAR
#include "MemMap.h"
# endif /*BRS_ENABLE_SUPPORT_LEDS*/


static uint8 BrsAsrApplCanInterruptLockCtr;

/**********************************************************************************************************************
  LOCAL CONST VARIABLES
**********************************************************************************************************************/
# if defined (_MICROSOFT_C_VTT_)
  #define BRSMAIN_ERRORBUFFERSIZE  120
#endif

/**********************************************************************************************************************
  PROTOTYPES OF GLOBAL FUNCTIONS
**********************************************************************************************************************/
#if defined (BRSASR_ENABLE_OS_STUB)
extern void Default_BSW_Async_Taskfunc(void);
#endif

/**********************************************************************************************************************
  PROTOTYPES OF LOCAL FUNCTIONS
**********************************************************************************************************************/


/**********************************************************************************************************************
  FUNCTION DEFINITIONS
**********************************************************************************************************************/

/*****************************************************************************/
/**
 * @brief      Main initialization routine.
 *             Contains initialisation of BRSModules and BrsMain specific initialization
 * @pre        -
 * @param[in]  -
 * @param[out] -
 * @return     -
 * @context    Function is called from main
 */
/*****************************************************************************/


/*****************************************************************************/
/**
 * @brief      InitTask to call EcuM_StartupTwo()
 * @pre        -
 * @param[in]  -
 * @param[out] -
 * @return     -
 * @context    The task is started once by the OS
 */
/*****************************************************************************/
TASK(Default_Init_Task)
{
#if defined (BRSASR_ENABLE_OS_MULTICORESUPPORT)
  /* Workaround for RTE ESCAN00078832 */
  /* Use this code, if you get a Det Error at the end of Rte_Start() on MasterCore */
  /* Rte_Start() on the SlaveCores has to be called first, before Rte_Start() on MasterCore */
  /* SET THIS InitTask TO FULL PREEMPTIVE (OsTaskSchedule) within OsConfig! */
  /*while(Rte_InitState_1 != RTE_STATE_INIT)
  {
    (void)Schedule();
  }*/
#endif /*BRSASR_ENABLE_OS_MULTICORESUPPORT*/

  


  EcuM_StartupTwo();

 
  #if 0
    {
      ISRType i;
      for (i = (ISRType)0; i < OS_ISRID_COUNT; i++)
      {
        Os_EnableInterruptSource(i, TRUE);
      }
    }
    #endif

        {
      ISRType i;
      for (i = (ISRType)0; i < OS_ISRID_COUNT; i++)
      {
        Os_EnableInterruptSource(i, TRUE);
      }
    }
  (void)TerminateTask();
}



/*****************************************************************************/
/**
 * @brief      This is the central exeption handler of BRS.
 *             All BRS modules and all CANbedded specific fatal error handler will
 *             call this API in case an assertion has failed.
 * @pre        -
 * @param[in]  ErrorCode shall hold the caller specific error code (uint8)
 * @param[in]  ModuleCode shall describe the caller; please use the CI constant as parameter (uint8)
 * @param[in]  LineNumber shall be the line where the assertion has failed, or, 
 *             if not available on caller location, the line where this API is 
 *             called from
 * @param[out] -
 * @return     -
 * @context    This function implements an endless loop with locked interrupts.
 *             The recommendation is to set a breakpoint on top of this function 
 *             to see if any assertion has failed during the code execution.
 *             Due to an assertion has failed and the endless loop runs with
 *             locked global interrupts there will be no life after the call ...
 */
/*****************************************************************************/


/*****************************************************************************/
/**
 * @brief      Main function
 * @pre        -
 * @param[in]  -
 * @param[out] -
 * @return     always 0 as the function is not expected to return
 * @context    Called by the startup code
 */
/*****************************************************************************/



int main(void)
{

//BrsHwWatchdogInitPowerOn();


  Os_InitMemory();
  Os_Init();


  EcuM_Init(); /* never returns */

  return 0;
}

/***********************************************************************************************************************
 *  Additional UserCode can be placed here
 **********************************************************************************************************************/


/***********************************************************************************************************************
 * UserDefined ExclusiveArea handling for CAN channels, according to AN-ISC-8-1149_ErrorHook_E_OS_DISABLED_INT.pdf
 **********************************************************************************************************************/
void SchM_Enter_Can_CAN_EXCLUSIVE_AREA_0(void)
{
  if (++BrsAsrApplCanInterruptLockCtr == 1u)
  {
    Can_DisableControllerInterrupts(0u);
  }
}

void SchM_Exit_Can_CAN_EXCLUSIVE_AREA_0(void)
{
  if (--BrsAsrApplCanInterruptLockCtr == 0u)
  {
    Can_EnableControllerInterrupts(0u);
  }
}

void SchM_Enter_Can_CAN_EXCLUSIVE_AREA_6(void)
{
  if (++BrsAsrApplCanInterruptLockCtr == 1u)
  {
    Can_DisableControllerInterrupts(0u);
  }
}

void SchM_Exit_Can_CAN_EXCLUSIVE_AREA_6(void)
{
  if (--BrsAsrApplCanInterruptLockCtr == 0u)
  {
    Can_EnableControllerInterrupts(0u);
  }
}

void SchM_Enter_CanIf_CANIF_EXCLUSIVE_AREA_0(void)
{
  if (++BrsAsrApplCanInterruptLockCtr == 1u)
  {
    Can_DisableControllerInterrupts(0u);
  }
}

void SchM_Exit_CanIf_CANIF_EXCLUSIVE_AREA_0(void)
{
  if (--BrsAsrApplCanInterruptLockCtr == 0u)
  {
    Can_EnableControllerInterrupts(0u);
  }
}

void SchM_Enter_CanSM_CANSM_EXCLUSIVE_AREA_4(void)
{
  if (++BrsAsrApplCanInterruptLockCtr == 1u)
  {
    Can_DisableControllerInterrupts(0u);
  }
}

void SchM_Exit_CanSM_CANSM_EXCLUSIVE_AREA_4(void)
{
  if (--BrsAsrApplCanInterruptLockCtr == 0u)
  {
    Can_EnableControllerInterrupts(0u);
  }
}

void SchM_Enter_CanSM_CANSM_EXCLUSIVE_AREA_1(void)
{
  /* Protects against task interruptions -> no locking needed, because all main functions are on same task */
}

void SchM_Exit_CanSM_CANSM_EXCLUSIVE_AREA_1(void)
{
  /* Protects against task interruptions -> no locking needed, because all main functions are on same task */
}

void SchM_Enter_ComM_COMM_EXCLUSIVE_AREA_1(void)
{
  /* Protects against task interruptions -> no locking needed, because all main functions are on same task */
}

void SchM_Exit_ComM_COMM_EXCLUSIVE_AREA_1(void)
{
  /* Protects against task interruptions -> no locking needed, because all main functions are on same task */
}

